<template xmlns:el-col="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择设备" prop="equipmentCode">
        <el-select v-model="queryParams.equipmentCode" placeholder="请选择">
          <el-option
            v-for="dict in equipmentList"
            :key="dict.gyroscopeCode"
            :label="dict.equipmentName"
            :value="dict.gyroscopeCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="数据时间" prop="dataTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['baseinfo:moreTemperatureData:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['baseinfo:moreTemperatureData:add']"
        >计算Tx</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moreTemperatureDataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="设备编号" align="center" prop="equipmentCode" />-->
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="温度5m" align="center" prop="temperature5" />
      <el-table-column label="温度10m" align="center" prop="temperature10" />
      <el-table-column label="温度15m" align="center" prop="temperature15" />
      <el-table-column label="温度20m" align="center" prop="temperature20" />
      <el-table-column label="温度25m" align="center" prop="temperature25" />
      <el-table-column label="温度30m" align="center" prop="temperature30" />
      <el-table-column label="温度35m" align="center" prop="temperature35" />
      <el-table-column label="温度40m" align="center" prop="temperature40" />
      <el-table-column label="温度45m" align="center" prop="temperature45" />
      <el-table-column label="温度50m" align="center" prop="temperature50" />
      <el-table-column label="时间" align="center" prop="dataTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['baseinfo:moreTemperatureData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改多点位温度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="calcForm" :model="calcForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="9">
            <el-form-item label="选择设备" prop="equipmentCode">
              <el-select v-model="calcForm.equipmentCode" placeholder="请选择">
                <el-option
                  v-for="dict in equipmentList"
                  :key="dict.gyroscopeCode"
                  :label="dict.equipmentName"
                  :value="dict.gyroscopeCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="15">
            <el-form-item label="数据时间" prop="dataTime">
              <el-date-picker
                v-model="calcForm.dateRange"
                size="small"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                style="width: 360px;"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数据条数">
               {{total1}}
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item style="float: right">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleChartQuery">搜索</el-button>
            </el-form-item>
          </el-col>

          <el-col>
            <line-chart-four v-if="total1>0" :chart-data="chartData" :check-list ="checkList" :legend-show = "true"></line-chart-four>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left">计算参数选择</el-divider>
          </el-col>
          <el-col :span="24">
            <el-form-item label="选择深度">
              <el-checkbox-group v-model="checkList">
                <el-checkbox label="5m"  key="5">温度5m</el-checkbox>
                <el-checkbox label="10m" key="10">温度10m</el-checkbox>
                <el-checkbox label="15m" key="15">温度15m</el-checkbox>
                <el-checkbox label="20m" key="20">温度20m</el-checkbox>
                <el-checkbox label="25m" key="25">温度25m</el-checkbox>
                <el-checkbox label="30m" key="30">温度30m</el-checkbox>
                <el-checkbox label="35m" key="35">温度35m</el-checkbox>
                <el-checkbox label="40m" key="40">温度40m</el-checkbox>
                <el-checkbox label="45m" key="45">温度45m</el-checkbox>
                <el-checkbox label="50m" key="50">温度50m</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计算标题" prop="calculateTitle">
              <el-input v-model="calcForm.calculateTitle" placeholder="请输入计算标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="导热系数K" prop="k">
              <el-input v-model="calcForm.k" placeholder="请输入导热系数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计算点高度H" prop="h">
              <el-input v-model="calcForm.h" placeholder="请输入高度值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地层导热系数λ" prop="lm">
              <el-input v-model="calcForm.lm" placeholder="请输入地层导热系数" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleCalculation">计 算</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMoreTemperatureData, getMoreTemperatureData, delMoreTemperatureData, addMoreTemperatureData, updateMoreTemperatureData } from "@/api/baseinfo/moreTemperatureData";
import {getEquipments} from "../../../api/baseinfo/equipment";
import {getTemperatureChartData} from "../../../api/baseinfo/moreTemperatureData";
import LineChartFour from '../../LineChart/LineChartFour.vue';
import {addCalculateTx} from "@/api/baseinfo/calculateTx";

export default {
  name: "MoreTemperatureData",
  components: {
    LineChartFour
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total1:0,
      // 多点位温度表格数据
      moreTemperatureDataList: [],
      chartData: {},
      checkList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      equipmentList:[],
      dateRange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentCode: null,
      },
      calcForm: {
        equipmentCode: null,
        calculateTitle:null,
        k:1.0,
        h:1.0,
        lm: 2.9,
        dateRange:[],
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        equipmentCode: [
          { required: true, message: "请选择设备", trigger: "change" }
        ],
        calculateTitle:[
          { required: true, message: "请填写计算标题", trigger: "blur" }
        ],
        k:[
          { required: true, message: "请填写导热系数K", trigger: "blur" }
        ],
        lm:[
          { required: true, message: "请输入地层导热系数λ", trigger: "blur" }
        ],
        h:[
          { required: true, message: "请填写计算点高度H", trigger: "blur" }
        ]
      }

    };
  },
  created() {
    this.getEquipmentList()
    this.getList();
  },
  methods: {
    /** 查询多点位温度列表 */
    getList() {
      this.loading = true;
      listMoreTemperatureData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.moreTemperatureDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleTemperatureChartData(){
      getTemperatureChartData(this.addDateRange(this.calcForm, this.calcForm.dateRange)).then(res =>{
        this.chartData = res.chartData;
        this.total1 = res.total;
        this.$modal.msgSuccess("查询成功");
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentCode: null,
        temperature5: null,
        temperature10: null,
        temperature15: null,
        temperature20: null,
        temperature25: null,
        temperature30: null,
        temperature35: null,
        temperature40: null,
        temperature45: null,
        temperature50: null,
        dataTime: null,
      };
      this.calcForm.dateRange =[];
      this.checkList=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleChartQuery() {
      this.handleTemperatureChartData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getEquipmentList() {
      getEquipments({equipmentType:"2"}).then(response => {
        this.equipmentList = response.data;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.total1 = 0;
      this.calcForm.equipmentCode = this.queryParams.equipmentCode;
      this.calcForm.dateRange = this.dateRange;
      this.open = true;
      this.title = "温度计算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMoreTemperatureData(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改多点位温度";
      });
    },
    //计算tx
    handleCalculation(){
      this.$refs["calcForm"].validate(valid => {
        if (valid) {
          this.calcForm.checkList = this.checkList;
          if(this.calcForm.checkList.length!=2){
            this.$modal.msgError("请选择2个探测点");
            return;
          }
          addCalculateTx(this.addDateRange(this.calcForm,this.calcForm.dateRange)).then(res =>{
            this.open = false;
          })

         /* calculation(this.addDateRange(this.calcForm,this.calcForm.dateRange)).then(res =>{

          })*/
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMoreTemperatureData(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMoreTemperatureData(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除多点位温度编号为"' + ids + '"的数据项？').then(function() {
        return delMoreTemperatureData(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('baseinfo/moreTemperatureData/export', {
        ...this.queryParams
      }, `moreTemperatureData_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
